package net.vengeancecraft.NoPortals;

import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPortalEvent;


public class NoPortalsPlayerListener implements Listener
{
    public static final Logger log = Logger.getLogger("Minecraft");
    public static NoPortals plugin;

    public NoPortalsPlayerListener(NoPortals plugin)
    {
        NoPortalsPlayerListener.plugin = plugin;         
    }    
    
        
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerPortal(PlayerPortalEvent event)
    {
       
        if (event.isCancelled()){return;}
        if (event.getTo() == null){return;} //added due to end portals sometimes existing but the End has been disabled or does not exist.
        Player player = event.getPlayer();
        if(NoPortals.watchDog) 
        {
            String  blockLocation;
            String blockLocationXMinusOne;
            String blockLocationXPlusOne;
            String blockLocationZMinusOne;
            String blockLocationZPlusOne;
            blockLocation = player.getLocation().getWorld().getName().toString() + "|" + Integer.toString(player.getLocation().getBlockX()) + "|" + Integer.toString(player.getLocation().getBlockY()) + "|" + Integer.toString(player.getLocation().getBlockZ());
            blockLocationXMinusOne = player.getLocation().getWorld().getName().toString() + "|" + Integer.toString(player.getLocation().getBlockX() - 1) + "|" + Integer.toString(player.getLocation().getBlockY()) + "|" + Integer.toString(player.getLocation().getBlockZ());
            blockLocationXPlusOne = player.getLocation().getWorld().getName().toString() + "|" + Integer.toString(player.getLocation().getBlockX() + 1) + "|" + Integer.toString(player.getLocation().getBlockY()) + "|" + Integer.toString(player.getLocation().getBlockZ());
            blockLocationZMinusOne = player.getLocation().getWorld().getName().toString() + "|" + Integer.toString(player.getLocation().getBlockX()) + "|" + Integer.toString(player.getLocation().getBlockY()) + "|" + Integer.toString(player.getLocation().getBlockZ() - 1);
            blockLocationZPlusOne = player.getLocation().getWorld().getName().toString() + "|" + Integer.toString(player.getLocation().getBlockX()) + "|" + Integer.toString(player.getLocation().getBlockY()) + "|" + Integer.toString(player.getLocation().getBlockZ() + 1);
            
            if(!NoPortals.validPortals.containsKey(blockLocation) && !NoPortals.validPortals.containsKey(blockLocationXMinusOne) && !NoPortals.validPortals.containsKey(blockLocationXPlusOne) && !NoPortals.validPortals.containsKey(blockLocationZMinusOne) && !NoPortals.validPortals.containsKey(blockLocationZPlusOne))
            {
                player.sendMessage(ChatColor.DARK_PURPLE + "This is not a valid portal.");
                if (NoPortals.destroyer)
                {
                    Block block = event.getFrom().getBlock();
                    if(block.getType() == Material.PORTAL){block.setType(Material.AIR);}
                    if(block.getRelative(1, 0, 0).getType() == Material.PORTAL){block.getRelative(1, 0, 0).setType(Material.AIR);}
                    if(block.getRelative(-1, 0, 0).getType() == Material.PORTAL){block.getRelative(-1, 0, 0).setType(Material.AIR);}
                    if(block.getRelative(0, 0, 1).getType() == Material.PORTAL){block.getRelative(0, 0, 1).setType(Material.AIR);}
                    if(block.getRelative(0, 0, -1).getType() == Material.PORTAL){block.getRelative(0, 0, -1).setType(Material.AIR);}
                    
                    
                   
                }
                event.setCancelled(true);
            } 
        }
        
        if (event.isCancelled()){return;}
        
        
        if(NoPortals.hasPerm(player, "noportals." + event.getTo().getWorld().getName().toLowerCase() + ".allow"))
        {
            NoPortals.togglePortals = true;
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {@Override public void run() {NoPortals.togglePortals = false;}}, 40);
        }
        
       
        
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerInteract(PlayerInteractEvent event)
    {
        if (event.isCancelled()){return;}
        
        Player player = event.getPlayer();
        if(event.getClickedBlock().getType() == Material.OBSIDIAN && event.getMaterial() == Material.FLINT_AND_STEEL)
        {
            if(NoPortals.hasPerm(player, "noportals." + player.getWorld().getName().toLowerCase() + ".allow"))
            {
               NoPortals.togglePortals = true;
               plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {@Override public void run() {NoPortals.togglePortals = false;}}, 40);
            }
            else
            {
                if(NoPortals.toggleCustomMessage)
                {
                    player.sendMessage(ChatColor.DARK_PURPLE + NoPortals.customMessage.toString());
                }
            }    
                
           
        }
    }    
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void enderBlockInteract(PlayerInteractEvent event)
    {
        if (event.isCancelled()) {return;}
        
        if (!NoPortals.stopEnder){return;}
        
        Player player = event.getPlayer();
        if(event.getClickedBlock().getType() == Material.ENDER_PORTAL_FRAME && event.getMaterial() == Material.EYE_OF_ENDER)
        {           
            if(!NoPortals.hasPerm(player, "noportals." + player.getWorld().getName().toLowerCase() + ".allow"))
            {
                
                if(NoPortals.toggleCustomMessage)
                {
                    player.sendMessage(ChatColor.DARK_PURPLE + NoPortals.customMessage.toString());
                }
                
                event.setCancelled(true);
                
            }
        }
        
    }
}